# -*- coding:utf-8 -*-

import pymysql
from .. import config
import time
import random
import bcrypt


def GetNowTime():
    return time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(time.time()))


def connectSQL():
    db = pymysql.connect(host=config.DatabaseIP,
                         port=config.Port,
                         user=config.UserName,
                         passwd=config.Password,
                         db=config.Database,
                         charset='utf8')
    return db


def VerifyUser(username, pwd):
    dbx = connectSQL()
    cursor = dbx.cursor()

    order = "SELECT password FROM users WHERE username = \"%s\"" % (username)
    cursor.execute(order)
    data = cursor.fetchall()

    dbx.close()

    try:
        if data[0][0] == pwd:
            return 0
        else:
            return 1
    except:
        dbx = pymysql.connect(host=config.DatabaseIP,
                              port=config.Port,
                              user=config.UserName,
                              passwd=config.Password,
                              db=config.Database_Ghost,
                              charset='utf8')
        cursor = dbx.cursor()
        order = "SELECT password FROM users WHERE email = \"%s\"" % (username)
        cursor.execute(order)
        data = cursor.fetchall()
        dbx.close()
        try:
            if bcrypt.checkpw(str(pwd), str(data[0][0])):
                return 0
            else:
                return 1
        except:
            return 2


def gencode(username, pwd):
    dbx = connectSQL()
    cursor = dbx.cursor()

    order = "SELECT password,authority FROM users WHERE username = \"%s\"" % (username)
    cursor.execute(order)
    data = cursor.fetchall()

    try:
        if data[0][0] == pwd:
            if data[0][1] == 1:
                tmp = random.sample('abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890', 8)
                code = ''.join(tmp)
                order = "INSERT INTO invite_code (code) VALUES (\'%s\')" % (str(code))
                cursor.execute(order)
                dbx.commit()
                dbx.close()
                return code
            else:
                return "authorityfail"
        else:
            return "pwdfail"
    except:
        return "userfail"


def UserRegister(username, pwd, incode):
    dbx = connectSQL()
    cursor = dbx.cursor()

    order = "SELECT code FROM invite_code WHERE code = \'%s\'" % (str(incode))
    cursor.execute(order)
    data = cursor.fetchall()
    if len(data) == 0:
        return 1
    else:
        order = "SELECT id FROM users WHERE username = \'%s\'" % (str(username))
        cursor.execute(order)
        data = cursor.fetchall()
        if len(data) != 0:
            return 2

        order = "SET @idx = (SELECT MAX(id) FROM users) + 1;"
        cursor.execute(order)
        order = "INSERT INTO users (id,username,password) VALUES (@idx,\'%s\',\'%s\');" % (username, pwd)
        cursor.execute(order)
        order = "DELETE FROM invite_code WHERE code = \'%s\'" % (str(incode))
        cursor.execute(order)
        dbx.commit()
        dbx.close()
        return 0
